package com.cloudptt.api.product.config.lbs;

import android.content.Context;
import android.content.SharedPreferences;

public class LocationConfig
{

	private static String THIS_FILE = "LocalConfig";
	private static final String LOCALE_SP_FILE = "locale_config";
	private static final String LOCALE_SWITCH = "switch";
	private static final String LOCALE_TYPE = "type";
	//	private static final String LOCALE_TIME_INTERVAL = "time_interval";
	//	private static final String LOCALE_INTERVAL = "interval";


	public static final String   POSITION_MODE = "positioning_mode";
	public static final String   POSITION_SWITCH = "positionging_switch";
	public static final String   POSITION_CHOICE_SWITCH = "positioning_choice_switch";
	public static final String   POSITION_TIME_INTERVAL = "positioning_time_interval";
	public static final String   POSITION_DISTANCE_INTERVAL = "positioning_distance_interval";
	//	public static final String   POSITION_SERVER_ADDR = "positioning_server_address";

	public static final String   POSITION_SERVER_SWITCH = "server_switch";
	public static final String   POSITION_TYPE = "positioning_type";

	public static final String SHARED_PROP_NAME = "name";
	public static final String SHARED_PROP_USERKEY = "userKey";

	public static final String SHARED_POSITIONING_SETTING = "positioning_setting";

	public final static int Type_None = 0;
	public final static int Type_GPS = 1;
	public final static int Type_Baidu = 2;

	private final static int Interval_default = 20;

	private SharedPreferences sp = null;

	private boolean isOpenLocation = true;
	private int locationType = Type_GPS;
	private int timeInterval = Interval_default;
	private int distanceInterval =Interval_default;

	private LocationConfig(boolean isOpenLocation,int locationType,int timeInterval,int distanceInterval)
	{
		this.isOpenLocation = isOpenLocation;
		this.locationType = locationType;
		this.timeInterval = timeInterval;
		this.distanceInterval = distanceInterval;
	}
	public LocationConfig(Context ctx)
	{
		this.sp = ctx.getSharedPreferences(LOCALE_SP_FILE, Context.MODE_PRIVATE);//PreferenceManager.getDefaultSharedPreferences(ctx);

		//		this.ip = sp.getString(LOCALE_IP, Ip_default);
		//		this.port = sp.getInt(LOCALE_PORT, Port_default);

		this.isOpenLocation = sp.getBoolean(LOCALE_SWITCH, true);
		this.locationType = sp.getInt(LOCALE_TYPE, Type_GPS);
		this.timeInterval = sp.getInt(POSITION_TIME_INTERVAL, Interval_default);
		this.distanceInterval = sp.getInt(POSITION_DISTANCE_INTERVAL, Interval_default);

		//		this.sp.registerOnSharedPreferenceChangeListener(this);
	}

	//	@Override
	//	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
	//			String key) 
	//	{
	//		// TODO Auto-generated method stub
	//		if(null == key)
	//		{
	//			Log.e(THIS_FILE,"onSharedPreferenceChanged key = null");
	//			return;
	//		}
	////		if(key.equals(LOCALE_IP)
	////				|| key.equals(LOCALE_PORT))
	////		{
	////			this.ip = sp.getString(LOCALE_IP, Ip_default);
	////			this.port = sp.getInt(LOCALE_PORT, Port_default);
	////			if(this.listener != null)
	////			{
	////				this.listener.onServerChange(ip, port);
	////			}
	////		}
	////		else 
	//		if(key.equals(LOCALE_SWITCH)
	//				|| key.equals(LOCALE_TYPE))
	//		{
	//			boolean newIsOpenLocation = sharedPreferences.getBoolean(key, true);
	//			int newLocationType = sharedPreferences.getInt(key, Type_GPS);
	//			
	//			boolean isChange = openOrTypeIsChange(newIsOpenLocation,newLocationType);
	//			if(isChange
	//					&& this.listener != null)
	//			{
	//				this.listener.onConfigChangeByUser();
	//			}
	//		}
	////		else if(key.equals(POSITION_TIME_INTERVAL)
	////				|| key.equals(POSITION_DISTANCE_INTERVAL))
	////		{
	////			int newtimeInterval = sharedPreferences.getInt(key, Interval_default);
	////			int newDistanceInterval = sharedPreferences.getInt(key, Interval_default);
	////			boolean isChange = intervalIsChange(newtimeInterval,newDistanceInterval);
	////			if(isChange
	////					&& this.listener != null)
	////			{
	////				this.listener.onConfigChange();
	////			}
	////		}
	//	}

	private boolean openOrTypeIsChange(boolean newIsOpenLocation,int newLocationType)
	{
		if( isOpenLocation !=newIsOpenLocation
				|| locationType != newLocationType)
		{
			isOpenLocation = newIsOpenLocation;
			locationType = newLocationType;
			return true;
		}
		return false;
	}

	private boolean intervalIsChange(int newtimeInterval,int newDistanceInterval)
	{
		if( timeInterval !=newtimeInterval
				|| distanceInterval != newDistanceInterval)
		{
			this.setTimeInterval(timeInterval);
			this.setDistanceInterval(distanceInterval);
			return true;
		}
		return false;
	}

//	public void updateConfig(String mcpttId,LocationInfoBody_Configuration body)
//	{
//		if(body != null)
//		{
//			int timeInterval = body.getTimeInterval();
//			int distanceInterval = body.getDistanceInterval();
//
//			this.setTimeInterval(timeInterval);
//			this.setDistanceInterval(distanceInterval);
////			boolean isChange = intervalIsChange(timeInterval,distanceInterval);
////			if(isChange
////					&& this.listener != null)
////			{
////				this.listener.onConfigChangeBySvr(mcpttId);
////			}
//		}
//	}


	public boolean isOpenLocation() {
		return isOpenLocation;
	}

	public void setOpenLocation(boolean isOpenLocation,int type) 
	{
		boolean isOpenLocationChange = (this.isOpenLocation != isOpenLocation);
		if(isOpenLocationChange)
		{
			this.isOpenLocation = isOpenLocation;
			SharedPreferences.Editor editor = sp.edit();
			editor.putBoolean(LOCALE_SWITCH, isOpenLocation);
			editor.commit();
		}
		boolean isOpenLocationTypeChange = (this.locationType != type);
		if(isOpenLocationTypeChange)
		{
			this.locationType = type;
			SharedPreferences.Editor editor = sp.edit();
			editor.putInt(LOCALE_TYPE, type);
			editor.commit();
		}
//		if((isOpenLocationChange
//				|| isOpenLocationTypeChange)
//				&& this.listener != null)
//		{
////			this.listener.onConfigChangeByUser();
//		}
	}

	public int getLocationType() {
		return locationType;
	}


	public int getTimeInterval() {
		return timeInterval;
	}

	public void setTimeInterval(int interval) {
		this.timeInterval = interval;
		SharedPreferences.Editor editor = sp.edit();
		editor.putInt(POSITION_TIME_INTERVAL, interval);
		editor.commit();
	}

	public boolean isSameInterval(LocationConfig newConfig)
	{
		int newTimeInterval = newConfig.getTimeInterval();
		int newDistanceInterval = newConfig.getDistanceInterval();
		if(newTimeInterval != timeInterval
				|| newDistanceInterval != distanceInterval)
		{
			return false;
		}
		return true;
	}


	public int getDistanceInterval() {
		return distanceInterval;
	}

	public void setDistanceInterval(int distanceInterval) {
		this.distanceInterval = distanceInterval;
		SharedPreferences.Editor editor = sp.edit();
		editor.putInt(POSITION_DISTANCE_INTERVAL, distanceInterval);
		editor.commit();
	}

//	public void setListener(ILocalConfigListener listener) {
//		this.listener = listener;
//	}

	public LocationConfig clone()
	{
		return new LocationConfig(isOpenLocation,locationType,timeInterval,distanceInterval);
	}

	public String toString()
	{
		StringBuffer buffer = new StringBuffer("LocationConfig:");
		buffer.append("isOpenLocation = ").append(isOpenLocation).append(",");
		buffer.append("locationType = ").append(locationType).append(",");
		buffer.append("timeInterval = ").append(timeInterval).append(",");
		buffer.append("distanceInterval = ").append(distanceInterval).append(";");

		return buffer.toString();
	}
}
